﻿
using imcactivex_x64Lib;
using Microsoft.Extensions.Configuration;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using System.IO;
using System.Text.Json;
using System.Xml.Linq;


namespace WheelClean2.Model
{
    public class MarkInfo : BindableBase
    {
        public IIMCAX64Mark Mark { get; set; }
        public IIMCAX64Edit Edit { get; set; }

        private CardInfo _cardInfo;
        public CardInfo CardInfo
        {
            get { return _cardInfo; }
            set { SetProperty(ref _cardInfo, value); }
        }

        private LaserInfo _laserList;
        /// <summary>
        /// 激光参数
        /// </summary>
        public LaserInfo LaserList
        {
            get { return _laserList; }
            set { SetProperty(ref _laserList, value); }
        }


        public WheelClean2.Mould.DaBiaoHelper DaBiaoHelper { get; set; }

        #region 激光属性
     


        public DelegateCommand OpenFileDialogCommand { get; private set; }
        public DelegateCommand SetParCommand { get; private set; }

        /// <summary>
        /// 开始
        /// </summary>
        public DelegateCommand StartCommand { get; private set; }
        /// <summary>
        /// 停止
        /// </summary>
        public DelegateCommand StopCommand { get; private set; }
        /// <summary>
        /// 结束
        /// </summary>
        public DelegateCommand FinishCommand { get; private set; }
        /// <summary>
        /// 红光预览开启
        /// </summary>
        public DelegateCommand PreviewStartCommand { get; private set; }

        /// <summary>
        /// 红光预览关闭
        /// </summary>
        public DelegateCommand PreviewStopCommand { get; private set; }

        #endregion


        public MarkInfo(WheelClean2.Mould.DaBiaoHelper daBiaoHelper,CardInfo cardInfo)
        {
            Mark = new IMCAX64MarkClass();
            Edit = new IMCAX64EditClass();
            LaserList = new LaserInfo();
            DaBiaoHelper = daBiaoHelper;
            CardInfo = cardInfo;
            // 读取为JsonDocument
            string jsonString = File.ReadAllText("config.json");
            using JsonDocument jsdoc = JsonDocument.Parse(jsonString);
            JsonElement root = jsdoc.RootElement;

            // 访问配置值
            string setting1 = root.GetProperty("连续ip").GetString();
            string path = CardInfo.CardIp.Contains(setting1) ? "C:\\iMark-x64" : "C:\\iMark-x64-2";
            LaserList.Type = CardInfo.CardIp.Contains(setting1) ? "连续激光" : "脉冲激光";
            int handle= daBiaoHelper.CreateSystem(CardInfo.CardId, path);
            Mark.Initial_MultiSystem(handle);
            int doc = Mark.CreateDocument();
            Mark.SetActiveDocument(doc);
            Edit.Initial_MultiSystem(handle);
            Edit.SetActiveDocument(doc);
            OpenFileDialogCommand = new DelegateCommand(OpenFileDialog);
            SetParCommand = new DelegateCommand(SetPar);
            StartCommand = new DelegateCommand(Start);
            StopCommand = new DelegateCommand(Stop);
            PreviewStartCommand = new DelegateCommand(PreviewStart);
            PreviewStopCommand = new DelegateCommand(PreviewStop);
        }

        public MarkInfo(CardInfo cardInfo)
        {
            CardInfo = cardInfo;
        }

        private void OpenFileDialog()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                string filePath = openFileDialog.FileName;

                //string path = "C:\\Users\\Administrator\\source\\repos\\DaBiaoControl\\DaBiaoControl\\3标准图形-细线.nmk";
                //Mark.LoadFile(filePath);
                LaserList.Path = filePath;
                LoadFile(filePath);
            }


        }

        /// <summary>
        /// 加载配置文件
        /// </summary>
        /// <param name="path"></param>
        public void LoadFile(string path)
        {
            Mark.LoadFile(path);
            Mark.SelectAllObjects();
            string name = "";
            Mark.SelectEnum(1, ref name);
            LaserList.Name = name;
            //速度
            LaserList.Speed = Mark.GetSpeed(name);
            //激光功率
             LaserList.Power = Mark.GetPower(name);
            //激光频率
             LaserList.Frequency = Mark.GetFrequency(name);
            //跳转速度
             LaserList.JumpSpeed = Mark.GetJumpSpeed(name);
            //跳转延迟 
             LaserList.JumpDelay = Mark.GetJumpDelay(name);
            //开光延迟 
            LaserList.LaserOnDelay = Mark.GetLaserOnDelay(name)/10;
            //关光延迟 
            LaserList.LaserOffDelay = Mark.GetLaserOffDelay(name)/10;
            //脉冲宽度 
            LaserList.PulseWidth = Mark.GetPulseWidth(name);
            //高度（线宽）
            LaserList.Height = Edit.GetHeight(name);
            //宽度
            LaserList.Width = Edit.GetWidth(name);
            bool enbale = false;
            double acceLength = 0;
            double deceLength = 0;
            Mark.GetSkyWriting(name,ref enbale, ref acceLength, ref deceLength);
            //是否开启打标模式
            LaserList.SkyIsEnble = enbale;
            //起点长度
            LaserList.AcceLength = acceLength;
            //结束点长度
            LaserList.DeceLength = deceLength;



        }


        public void SetPar()
        {
            if(string.IsNullOrEmpty(LaserList.Name))
                return;
            //速度
            Mark.SetSpeed(LaserList.Name, LaserList.Speed==null?0: LaserList.Speed.Value);
            //激光功率
            Mark.SetPower(LaserList.Name, LaserList.Power == null ? 0 : LaserList.Power.Value);
            //激光频率
            Mark.SetFrequency(LaserList.Name, LaserList.Frequency == null ? 0 : LaserList.Frequency.Value);
            //跳转速度
            Mark.SetJumpSpeed(LaserList.Name, LaserList.JumpSpeed == null ? 0 : LaserList.JumpSpeed.Value);
            //跳转延迟 
            Mark.SetJumpDelay(LaserList.Name, (int)LaserList.JumpDelay);
            //开光延迟 
            Mark.SetLaserOnDelay(LaserList.Name, (int)LaserList.LaserOnDelay * 10);
            //关光延迟 
            Mark.SetLaserOffDelay(LaserList.Name, (int)LaserList.LaserOffDelay * 10);
            //脉冲宽度 
            Mark.SetPulseWidth(LaserList.Name, LaserList.PulseWidth == null ? 0 : LaserList.PulseWidth.Value);
            //高度（线宽）  宽度
            Edit.SetDimension(LaserList.Name, LaserList.Width == null ? 0 : LaserList.Width.Value, LaserList.Height == null ? 0 : LaserList.Height.Value,0);
            //是否开启打标模式  起点长度 结束点长度
            Mark.SetSkyWriting(LaserList.Name, LaserList.SkyIsEnble, LaserList.AcceLength == null ? 0 : LaserList.AcceLength.Value, LaserList.DeceLength == null ? 0 : LaserList.DeceLength.Value);
            Mark.SaveFile(LaserList.Path);
        }

        public void Start()
        {
            Mark.PreviewStop();
            //if(CardInfo.CardIp.Contains("44.22"))
            //{
            //    int sm = Mark.SetMarkingMode(4);
            //}
            //else
            //{
            //    int sm = Mark.SetMarkingMode(3);
            //}
            int sm = Mark.SetMarkingMode(4);
            int ms=  Mark.MarkStandBy();
          int ism = Mark.IsMarkStandBy();
          int smk = Mark.StartMarking(2);
        }

        public void Stop()
        {
            Mark.MarkShutdown();
        }
        /// <summary>
        /// 预览开始
        /// </summary>
        public void PreviewStart()
        {
           var rsult= Mark.PreviewStart();
        }
        //预览关闭
        public void PreviewStop()
        {
            Mark.PreviewStop();
        }

        public void Finish()
        {
            Mark.Finish();
        }
    }
}
